<?php
date_default_timezone_set('Europe/Amsterdam');
include ('../classes/cFunctions.php');
include ('../../configs/cms.php');
include ('../includes/index.php');
include ('../includes/404.php');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if ($_SESSION['which'] == 'choose1')
	{
		require( '../includes/swiftmailer/swift_required.php');
		include_once ('../classes/cModel.php');
		
		$newsletter = new cModel ('nieuwsbrief', 'nieuwsbrief_id', $_POST ['nieuwsbrief']);

		$transport = Swift_SmtpTransport::newInstance($_CONFIG ['mail'] ['smtp'] ['server'], $_CONFIG ['mail'] ['smtp'] ['port'], $_CONFIG ['mail'] ['smtp'] ['ssl'])
			-> setUsername ($_CONFIG ['mail'] ['username'])
			-> setPassword ($_CONFIG ['mail'] ['password']);

		$mailer = Swift_Mailer::newInstance($transport);

		$_GET ['id'] = $_POST ['nieuwsbrief'];

		$aReplace = array ();
		$user = new cModel ('user', 'email');
		$user = $user -> getAll ();

		for ($i = 0; $i < count ($user); $i++)
		{
			$aReplace [$user [$i] -> email] = array (
				'{gebruikersnaam}'	=> $user [$i] -> gebruikersnaam,
				'{voornaam}'		=> $user [$i] -> voornaam,
				'{achternaam}'		=> $user [$i] -> achternaam,
				'{email}'			=> $user [$i] -> email,
				'{email_encoded}'	=> rawurlencode (base64_encode ($user [$i] -> email))
			);
		}

		$decorator = new Swift_Plugins_DecoratorPlugin ($aReplace);
		$mailer -> registerPlugin ($decorator);

		$swiftmailer = Swift_Message :: newInstance ()
			-> setSubject	($_CONFIG ['sitename'] . ': ' .  $newsletter -> title)
			-> setFrom		(array ('webmasters@cyberweekend.info' => 'Webmasters Cyberweekend'))
			-> setSender	(array ('webmasters@cyberweekend.info' => 'Webmasters Cyberweekend'))
			-> setBody		(cFunctions :: getIncludeContents ('../../public/layouts/cyberweekend/nieuwsbrief/index.php'), 'text/html');

		$user = new cModel ('user', 'user_id');
		$user = $user -> getAll ();
		die();
		
/*		for ($i = 0; $i < count ($user); $i++)
 		{
			$swiftmailer -> setTo (array ($user [$i] -> email => $user [$i] -> gebruikersnaam . ' (' . $user [$i] -> voornaam . ' ' . $user [$i] -> achternaam . ')'));
			$result = $mailer->send($swiftmailer);

			if ($user [$i] -> subscribed == 1)
			{
				if ($result == 1)
				{
					echo 'Nieuwsbrief versturing gelukt: ' . $user [$i] -> email . '<br />';
				}
				else
				{
					echo 'Nieuwsbrief versturing mislukt: ' . $user [$i] -> email . '<br />';
				}
			}
			else
			{
				echo 'Nieuwsbrief wil gebruiker niet ontvangen: ' . $user [$i] -> email . '<br />';
			}
		} */
	}
}
else
{
?>
	<div style="margin-left: 40px;"><form method="POST" action="">
	Welke nieuwsbrief wilt u versturen:<br />
	<?php
	$_SESSION['which'] = 'choose1';
	$query = mysql_query ("SELECT `nieuwsbrief_id`, `title` FROM `nieuwsbrief`;");
	echo '<select name="nieuwsbrief">';
	while ($result = mysql_fetch_assoc ($query))
	{
		echo '<option value="' . $result['nieuwsbrief_id'] . '">' . $result['title'] . '</option>' . "\n";
	}
	echo '</select>';
	?>
	<br /><br />
	<?php echo '<input type="submit" value="Verstuur nieuwsbrief">&nbsp;&nbsp;' ?><input type="reset" value="Leeg formulier"></form></div>
	<h1 style="margin-left:40px"><strong>Let op</strong>: <!--De nieuwsbrief kan nog niet worden verstuurd aangezien mensen zich nog niet kunnen uitschrijven!-->De nieuwsbrief wordt bij de klik METEEN(!!!) verzonden!</h1>
<?php
}